package com.naiterui.ehp.bs.b2c.order.controller.inner;

import com.naiterui.ehp.bp.bo.b2c.order.RefundOrderBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bs.b2c.order.exception.OrderBusinessException;
import com.naiterui.ehp.bs.b2c.order.exception.OrderExceptionCodes;
import com.naiterui.ehp.bs.b2c.order.service.IRefundOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Set;

@Slf4j
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.ORDER)
@Validated
@RestController
@RequestMapping("b2c/inner/refund")
public class InnerRefundOrderController {

    @Autowired
    private IRefundOrderService refundOrderService;

    /**
     * 保存更新
     *
     * @param submit 0：不提交  1：提交
     */
    @ApiOperation(value = "保存更新退款订单")
    @ApiImplicitParam(name = "submit", value = "0：不提交  1：提交", paramType = "path", dataType = "int", required = true)
    @PostMapping("{submit}")
    public ResponseVO<Void> update(@RequestBody @Valid RefundOrderBO refundOrderBO, @NotNull(message = "是否提交不能为null") @PathVariable Integer submit,
                                   @RequestHeaderDecode String operator) throws BusinessException {
        log.info("保存更新 请求参数：refundOrderBO = {}, submit = {}, operator = {}", refundOrderBO, submit, operator);
        refundOrderService.update(refundOrderBO, submit, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除退款订单")
    @ApiImplicitParam(name = "refundOrderId", value = "退款订单ID", paramType = "path", dataType = "long", required = true)
    @DeleteMapping("/{refundOrderId}")
    public ResponseVO<Void> del(@PathVariable Long refundOrderId, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("删除退款订单 请求参数：refundOrderId = {} operator = {}", refundOrderId, operator);
        refundOrderService.del(refundOrderId, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 审核
     */
    @ApiOperation(value = "审核退款订单")
    @ApiImplicitParam(name = "refundOrderIds", value = "退款订单ID集合", paramType = "body", dataType = "array",
            required = true)
    @PostMapping("/audit")
    public ResponseVO<Void> audit(@RequestBody Set<Long> refundOrderIds, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("审核退款订单 请求参数：refundOrderIds = {} operator = {}", refundOrderIds, operator);
        refundOrderService.audit(refundOrderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 反审核
     */
    @ApiOperation(value = "反审核退款订单")
    @ApiImplicitParam(name = "refundOrderIds", value = "退款订单ID集合", paramType = "body", dataType = "array",
            required = true)
    @PostMapping("/reverseAudit")
    public ResponseVO<Void> reverseAudit(@RequestBody Set<Long> refundOrderIds, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("反审退款订单 请求参数：refundOrderIds = {} operator = {}", refundOrderIds, operator);
        refundOrderService.reverseAudit(refundOrderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 确认付款
     */
    @ApiOperation(value = "确认付款")
    @ApiImplicitParam(name = "refundOrderIds", value = "退款订单ID集合", paramType = "body", dataType = "array",
            required = true)
    @PostMapping("/pay")
    public ResponseVO<Void> pay(@RequestBody Set<Long> refundOrderIds, @RequestHeaderDecode String operator) throws OrderBusinessException {
        log.info("确认付款退款订单 请求参数：refundOrderIds = {} operator = {}", refundOrderIds, operator);
        refundOrderService.pay(refundOrderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 作废
     */
    @ApiOperation(value = "作废退款订单")
    @ApiImplicitParam(name = "refundOrderIds", value = "退款订单ID集合", paramType = "body", dataType = "array",
            required = true)
    @PostMapping("/invalid")
    public ResponseVO<Void> invalid(@RequestBody Set<Long> refundOrderIds, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("作废退款 请求参数：refundOrderIds = {} operator = {}", refundOrderIds, operator);
        refundOrderService.invalid(refundOrderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 提交
     */
    @ApiOperation(value = "提交退款订单")
    @ApiImplicitParam(name = "refundOrderIds", value = "退款订单ID集合", paramType = "body", dataType = "array",
            required = true)
    @PutMapping("/submit")
    public ResponseVO<Void> submit(@RequestBody Set<Long> refundOrderIds, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("提交退款订单 请求参数：refundOrderIds = {} operator = {}", refundOrderIds, operator);
        refundOrderService.submit(refundOrderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

}
